package com.example.demo.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;

import com.example.demo.User;

@Mapper
public interface UserMapper {

	// 使用Map
	@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER})")
	int insertByMap(Map<String, Object> map);

	// 使用对象
	@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
	int insertByUser(User user);

	// 查询
	@Cacheable(key="'user_'+#uid",value="userCache")
	@Select("SELECT * FROM user WHERE name = #{name}")
	User findByName(@Param("name") String name);

	// 新增
	@CacheEvict(key="'user'",value="userCache")
	@Insert("INSERT INTO user(name, age) VALUES(#{name}, #{age})")
	int insert(@Param("name") String name, @Param("age") Integer age);

	// 修改
	@CachePut(key="'user_'+#user.uid", value="userCache")
	@Update("UPDATE user SET age=#{age} WHERE name=#{name}")
	void update(User user);

	// 删除
	@CacheEvict(key="'user_'+#uid", value="userCache")
	@Delete("DELETE FROM user WHERE id =#{id}")
	void delete(Long id);
	
	//返回结果的绑定
	@Select("SELECT name, age FROM user")
	@Results({ @Result(property = "name", column = "name"), @Result(property = "age", column = "age") })
	List<User> findAll();
}
